//package com.neudu.glsb.mapper;
//
//import com.neudu.glsb.pojo.OperationRecords;
//import com.neudu.glsb.pojo.Orders;
//import com.baomidou.mybatisplus.core.mapper.BaseMapper;
//import io.lettuce.core.dynamic.annotation.Param;
//import org.apache.ibatis.annotations.Insert;
//import org.apache.ibatis.annotations.Mapper;
//import org.apache.ibatis.annotations.Select;
//
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//
///**
//* @author Lenovo
//* @description 针对表【orders(订单信息)】的数据库操作Mapper
//* @createDate 2025-04-16 22:28:32
//* @Entity com.neudu.glsb.pojo.Orders
//*/
//@Mapper
//public interface OrdersMapper extends BaseMapper<Orders> {
//    // 统计每日订单总金额
//    @Select("SELECT DATE(order_time) as date, SUM(total_amount) as total_amount FROM orders GROUP BY DATE(order_time)")
//    List<Map<String, Object>> getDailyOrdersAmount();
//    @Select("SELECT DATE_FORMAT(order_time, '%Y-%m') as month, SUM(total_amount) as total_amount FROM orders GROUP BY DATE_FORMAT(order_time, '%Y-%m')")
//    List<Map<String, Object>> getMonthlyOrdersAmount();
//    // 查询订单状态为“完成”的订单
//    List<Orders> selectCompletedOrders();
//    /**
//     * 查询所有待取餐订单（大屏叫号用，按取餐码升序）
//     */
//    @Select("SELECT * FROM orders WHERE order_status = '待取餐' ORDER BY pickup_code ASC")
//    List<Orders> selectPendingPickupOrders();
//
//
//}
//
//


// com.neudu.glsb.mapper.OrdersMapper.java
package com.neudu.glsb.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.neudu.glsb.pojo.Orders;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

public interface OrdersMapper extends BaseMapper<Orders> {
    // 添加月度统计方法
    @Select("SELECT DATE_FORMAT(order_time, '%Y-%m') as month, SUM(total_amount) as total_amount " +
            "FROM orders " +
            "GROUP BY DATE_FORMAT(order_time, '%Y-%m') " +
            "ORDER BY month")
    List<Map<String, Object>> getMonthlyOrdersAmount();

    // 查询已完成订单（状态统一为"已完成"）
    @Select("SELECT * FROM orders WHERE order_status = '已完成'")
    List<Orders> selectCompletedOrders();

    // 新增：查询待取餐订单（按取餐码升序）
    @Select("SELECT * FROM orders WHERE order_status = '待取餐' ORDER BY pickup_code ASC")
    List<Orders> selectPendingPickupOrders();

    // 按日期统计订单金额
    @Select("SELECT DATE(order_time) as date, SUM(total_amount) as total_amount FROM orders GROUP BY DATE(order_time)")
    List<Map<String, Object>> getDailyOrdersAmount();
    //新增加
    //新增加
    //新增加
    // 按时间范围统计销售额趋势（按日分组）
    @Select("SELECT DATE(order_time) as date, SUM(total_amount) as amount " +
            "FROM orders " +
            "WHERE order_time BETWEEN #{startTime} AND #{endTime} " +
            "GROUP BY DATE(order_time) " +
            "ORDER BY DATE(order_time)")
    List<Map<String, Object>> getSalesTrendByDateRange(String startTime, String endTime);

    // 按时间范围统计支付方式占比
    @Select("SELECT payment_method as method, SUM(total_amount) as amount " +
            "FROM orders " +
            "WHERE order_time BETWEEN #{startTime} AND #{endTime} " +
            "GROUP BY payment_method")
    List<Map<String, Object>> getPaymentRatioByDateRange(String startTime, String endTime);

    // 按时间范围统计总数据（销售额、订单数）
    @Select("SELECT SUM(total_amount) as total_sales, COUNT(id) as order_count " +
            "FROM orders " +
            "WHERE order_time BETWEEN #{startTime} AND #{endTime}")
    Map<String, Object> getTotalStatsByDateRange(String startTime, String endTime);
}
